//
//  YXReleaseHeaderView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/27.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXReleaseHeaderView.h"

@interface YXReleaseHeaderView ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *textLab;
@property (weak, nonatomic) IBOutlet UILabel *numberLab;

@end
@implementation YXReleaseHeaderView

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        // 从xib中找到我们定义的view
        NSArray *viewArray = [[NSBundle mainBundle]loadNibNamed:@"YXReleaseHeaderView" owner:self options:nil];
        self = viewArray[0];
        self.frame = frame;
        self.backgroundColor = color_backColor;
        
        self.titleView.delegate = self;
    }
    return self;
    
}

#pragma mark - textView Delegate
//-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
//
//    self.textLab.text = nil;
//    return YES;
//}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
    
    [textView resignFirstResponder];
    [textView endEditing:YES];
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    self.textLab.hidden = YES;
    if (textView.text.length == 0) {
        self.textLab.hidden = NO;
    }
    if (textView.markedTextRange == nil && [textView.text length] > 50)
    {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 50)];
        [textView.undoManager removeAllActions];
        [textView becomeFirstResponder];
        //控制输入文本的长度
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"最大文字超限" message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *alert = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [alertController addAction:alert];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
        
        
        return;
    }

}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if ([text isEqualToString:@"\n"]) {
        //禁止输入换行
        return NO;
    }
    else{
        return YES;
        
    }
}

@end
